import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import axios from 'axios';
import store from "./store/index";
import Vant from 'vant';
import { Lazyload } from 'vant';
import 'vant/lib/vant-css/index.css';

import './utils/rem'

import { setStore, getStore, removeStore } from './utils/store'

import VueSocketio from 'vue-socket.io';
import socketio from 'socket.io-client';
//与服务端链接
// Vue.use(VueSocketio, socketio('127.0.0.1:3000'));


// http request 请求拦截器，有token值则配置上token值
axios.interceptors.request.use(function (config) {
  let uid = getStore('user_id');
  if (uid) {// 每次发送请求之前判断是否存在token，如果存在，则统一在http请求的header都加上token，不用每次请求都手动添加
    config.headers.common['Authorization'] = uid
  }
  return config
}, function (error) {
  return Promise.reject(error)
})


router.beforeEach((to, from, next) => {
  let uid = getStore('user_id');
  if (to.matched.some(record => record.meta.requireLogin)) {
    if (!uid) {
      next();
      router.push({ path: '/auth/login' })
    } else {
      next()
    }
  } else {
    next()
  }
});


Vue.use(Vant);
Vue.use(Lazyload);
Vue.config.productionTip = false;


new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
